<?php
/**
 * vim: et ts=4 sts=4 sw=4
 *
 * 文件上传类.
 *
 * @author	Wang Penghai <china_php@hotmail.com>
 * @copyright 2007 5e6d Corporation.
 * @version   $Id$
 */


/**
 *
 * 错误代码说明:
 * <code>
 * </code>
 *
 * @package DB_INTERFACE_SYS
 */

class pf_upload 
{
	/**
	 * 保存信息数组
	 */
	protected $__msg = array();
	/**
	 * 配置文件路径
	 *
	 * @var string
	 */
	protected $__user_upload = './';

	/**
	 * 配置允许上传文件大小
	 * @desc 默认10M
	 * @var int
	 */
	protected $__user_upload_size = null; 

	/**
	 * 配置允许上传文件类型
	 * @var array
	 */
	protected $__user_upload_type = array('jpg', 'gif', 'png',); 

	/**
	 * upload类报错收集
	 *
	 * @param $code $msg
	 *
	 * @return void
	 */
	protected function _set_error_info($code, $msg) 
	{
		$argvs = array(
				'code' => $code,
				'msg' => $msg,
				);
		if (empty($this->__msg)) {
			$this->__msg = $argvs;
		}else {
			$this->__msg[] = $argvs;
		}	
	}

	/**
	 * upload类报错打印
	 *
	 * @return array 错误信息
	 */
	public function get_error_info() 
	{
		return $this->__msg; 
	}

	/**
	 * 构造函数
	 *
	 * @return void
	 */
	public function __construct($size = 10)
	{
		$this->__user_upload_size = 1024*1024*$size;
	}
	
	/**
	 * 构造函数 php4
	 *
	 * @return void
	 */
	public function pf_lib_upload($size = 10)
	{
		$this->__construct($size);
	}
	/**
	 * 设置上传路径
	 *
	 * @param: array $path 路径
	 *
	 * @return void
	 */
	public function set_upload_path($path)
	{
		if (!empty($path)) {
			$this->__user_upload = $path;
		}
	}
	
	/**
	 * 设置上传类型
	 *
	 * @param: array $array 类型数组
	 *
	 * @return void
	 */
	public function set_upload_type($array)
	{
		if (!empty($array) && is_array($array)) {
			$this->__user_upload_type = $array;
		}
	}

	/**
	 * 上传类型过滤
	 *
	 * @param string $filetype 文件类型
	 *
	 * @return  成功 true |失败 false
	 */
	protected function _upload_check(&$filetype = null, &$filesize = null)
	{
		//echo $filetype;
		//echo '<br> dddd';
		if(!in_array($filetype, $this->__user_upload_type)) {
			$this->_set_error_info('107000','[_upload_check]: file type wrong!' . $filetype);
			return false;
		}
		
		if ($filesize > $this->__user_upload_size) {
			$this->_set_error_info('107001','[_upload_check]: The size  of the file is more !');
			return false;
		}
		
		return true;
	}
	
	/**
	 * 获取上传文件的后缀名称
	 *
	 * @param string $file 文件名称
	 */
	protected function _get_file_extension($file)
	{
		 return strtolower( str_replace( ".", "", substr( $file, strrpos( $file, '.' ) ) ) );
	}

	 
	/**
	 * 检查上传文件的保存路径
	 *
	 * @param string $filepath 文件路径
	 *
	 * @return  成功 true |失败 false
	 */
	protected function _upload_check_path($filepath, $filename)
	{
		$filepath = $this->__user_upload.$filepath;
		if (!is_dir($filepath)) {
			if (false === @mkdir ($filepath, 0700, true)) {
				$this->_set_error_info('107010', '[check_path] mkdir '. $filepath.'fail!');
				return false;
			}
		}
		
		if (is_file($filepath . $filename)) {
			return false;
		}
		
		return true;
	}
	 
	/**
	 * 上传文件主函数
	 *
	 * @param string $file 数组
	 *
	 * @return  成功 true |失败 false
	 */
	public function upload($file, $username='', $userid='0',$is_md5=false)
	{
		//var_export($file);
		if (null === $file || empty($file['tmp_name'])) {
			$this->_set_error_info('107020','[upload] param is empty!');
			return false;
		}
		
		$file_name = $file['name'];
		$file_size = $file['size'];
		$file_type = $file['type'];
		$file_tmp_name = $file['tmp_name'];
		$file_error = $file['error'];
		$file_ext = $this->_get_file_extension($file_name);
		if ($file_error>0) {
			$this->_set_error_info('107021','[upload]: upload fail!');
			return false;
		}
		if (false === $this->_upload_check($file_ext,$file_size)) {
			return false;
		}
		
		$time = time();
		$filepath = $username . '/' . date('ymd') . '/';	
		$filename = $userid . '_' . $time . '_' . str_pad (rand(),6,'0',STR_PAD_LEFT);
		if ($is_md5) {
			$filename = md5($filename);
		}
		 
		$filename .= '.'.$file_ext;
		if (false === $this->_upload_check_path($filepath, $filename)) {
			return false;
		}
		
		if (false === $this->_upload_do($file_tmp_name, $filepath . $filename)) {
			return false;
		}
		/* save to upfile*/

		$values['f_filename'] = $filepath . $filename;
		$values['f_filetype'] = $file_ext;
		$values['f_filesize'] = $file_size;
		$values['f_uptime'] = $time;
		$values['f_u_id'] = $userid;
		return $values;
		 
	}

	/**
	 * move_uploadpf_file
	 *
	 * @param string $file_tmp_name, $filename
	 *
	 * @return  成功 true |失败 false
	 */
	protected function _upload_do($file_tmp_name, $filename)
	{
		$filename = $this->__user_upload . $filename;
		if (false === @move_uploaded_file($file_tmp_name, $filename)) {
			$this->_set_error_info('107030','move_uploadpf_file fail!');
			return false;
		}
		return true;
	}

	/**
	 * 删除所上传的文件
	 *
	 * @param string $file_tmp_name, $filename
	 *
	 * @return  成功 true |失败 false
	 */
	 public function unlink_upload($filename)
	 {
	 	$filename = $this->__user_upload . $filename;
		if (false === @unlink($filename)) {
			$this->_set_error_info('107040','unlink file fail!');
			return false;
		}
		return true;
	}

};
/** 用例
include_once PF_APP_LIB_PATH . 'pf_lib_upload.class.php';

$upload = new pf_lib_upload;
$type = array('jpg','gif','png',);
$upload->set_upload_type($type);//设置上传文件类型
$upload->set_upload_path('./');//路径
$upload_info = $upload->upload($_FILES['FILE_UPLOAD'], 'images', 0,$f_r_id =0);//上传
//判断
if (false === $upload_info) {
	$error_info = $upload->get_error_info();
	switch($error_info['code']) {
		case '107000':
			$uploadStat = '此类型不允许上传！';
			break;
		case '107001':
			$uploadStat = '文件大小超过了10M！';
			break;
		default:
			$uploadStat = '系统错误， 不允许上传！';
			break;
	}

	echo "<script>";
	echo "alert('$uploadStat');";
	echo "</script>";
} else {
	$file_path = PATH_WEB_UPLOAD . $upload_info['f_filename'];
	echo "<script>";
	echo "parent.document.getElementById('imgUrl').value = '$file_path';";
	echo "parent.document.getElementById('fileupload').value = '';";
	echo "parent.createImg();";
	echo "</script>";
}
*/
?>
